package com.jichaoyun.sys.controller;


import com.jichaoyun.common.model.PagedResult;
import com.jichaoyun.common.model.Result;
//import com.jichaoyun.model.vo.MultiRealInfoVo;
import com.jichaoyun.model.vo.MultiRealInfoVo;

import com.jichaoyun.sys.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = {"多用户信息接口"})
@RequestMapping("/sys/MultiUserInfo")
public class MultiUserController {
    @Autowired
    private IStationInfoService stationInfoService;

    @Autowired
    private IRealdataService realdataService;

    @Autowired
    private LineInfoService lineInfoService;

    @Autowired
    private ILowParaConfigueService lowParaConfigueService;

    @Autowired
    private IFlowMonthService flowMonthService;

    @Autowired
    private IFlowDayService flowDayService;



    @Autowired
    private IRealdataCacheService realdataCacheService;

    @ApiOperation("多用户信息，用户数量，查询各线路用户分布情况")
    @GetMapping("/getUserLineDistribution")
    public Result<Map<String,Integer>> getUserLineDistribution() {
        return Result.success(stationInfoService.getUserLineDistribution());
    }

    @ApiOperation("多用户信息，用户数量占比，查询用户类别情况")
    @GetMapping("/getUserCategory")
    public Result<Map<String,Integer>> getUserCategory() {
        return Result.success(stationInfoService.getUserCategory());
    }

    @ApiOperation("多用户信息，报警用户占比，查询用户报警占比情况")
    @GetMapping("/getUserAlarmProportion")
    public Result<Map<String,Long>> eachAlarmProportion() {
        return Result.success(realdataService.eachAlarmProportion());
    }

    @ApiOperation("多用户信息，今日流量分布，查询昨日线路流量分布情况")
    @GetMapping("/getLineFlowDayProportion")
    public Result<Map<String,Float>> getLineFlowDayProportion() {
        return Result.success(lineInfoService.getLineFlowDay());
    }

    @ApiOperation("多用户信息，本月流量分布，查询月份线路流量分布情况")
    @GetMapping("/getLineFlowMonthProportion")
    public Result<Map<String,Float>> getLineFlowMonthProportion() {
        return Result.success(lineInfoService.getLineFlowMonth());
    }

    @ApiOperation("多用户信息，本年流量分布，查询年度线路流量分布情况")
    @GetMapping("/getLineFlowYearProportion")
    public Result<Map<String,Float>> getLineFlowYearProportion() {
        return Result.success(lineInfoService.getLineFlowYear());
    }
    @ApiOperation("多用户信息，今日流量排名，查询昨日流量排名")
    @GetMapping("/getDayFlowRank")
    public Result<Map<String,Map<Float,Integer>>> getDayFlowRank() {
        return Result.success(lineInfoService.getDayFlowRank());
    }

    @ApiOperation("多用户信息，本月流量排名，查询月份流量排名")
    @GetMapping("/getMonthFlowRank")
    public Result<Map<String,Map<Float,Integer>>> getMonthFlowRank() {
        return Result.success(lineInfoService.getMonthFlowRank());
    }

    @ApiOperation("多用户信息，本年流量排名，查询年度流量排名")
    @GetMapping("/getYearFlowRank")
    public Result<Map<String,Map<Float,Integer>>> getYearFlowRank() {
        return Result.success(lineInfoService.getYearFlowRank());
    }


    @ApiOperation("多用户信息，前月用气，对比上月与前月用气量")
    @GetMapping("/getGasCompareMonth")
    public Result<Map<String,Float>> getGasCompareMonth(Integer station_no) {
        return Result.success(flowMonthService.getMonthSteamComparison(station_no));
    }

    @ApiOperation("多用户信息，上年同月用气，对比上月与去年同月份用气量")
    @GetMapping("/getSameMonthSteamComparison")
    public Result<Map<String,Float>> getSameMonthSteamComparison(Integer station_no) {
        return Result.success(flowMonthService.getSameMonthSteamComparison(station_no));
    }

    @ApiOperation("多用户信息，多用户实时数据，查询多用户实时信息列表")
    @GetMapping("/getMultiRealInfoList")
    public Result<PagedResult> getMultiRealInfoList(Integer pageNo, Integer pageSize) {
        PagedResult pagedResult = realdataCacheService.getMultiRealInfovo(pageNo,pageSize);
        List<MultiRealInfoVo> multiRealInfoVoList = new ArrayList<>();
        //进行到vo返回的转化
        for (Object row : pagedResult.getRows()) {
            MultiRealInfoVo infoVo = new MultiRealInfoVo();
            BeanUtils.copyProperties(row,infoVo);
            multiRealInfoVoList.add(infoVo);
        }
        pagedResult.setRows(multiRealInfoVoList);
        return Result.success(pagedResult);
    }


    /**
     * zz
     * @param date
     * @param pageNo
     * @return
     */
    @ApiOperation(value = "多用户信息，多用户日报表，分页查询全部用户，根据日期获取",notes = "date格式yyyy-MM-dd，默认查10条数据")
    @GetMapping("/getMultiStationDayFormPage")
    public Result<PagedResult> getMultiStationDayFormPage(String date,Integer pageNo){

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date1 = LocalDate.parse(date,dateTimeFormatter);
        LocalDateTime theDay = date1.atStartOfDay();

        PagedResult pagedResult =  flowDayService.getAllMultiStationDayFormPage(theDay,pageNo);

        return Result.success(pagedResult);
    }

    @ApiOperation(value = "多用户信息，多用户日报表，导出全部的用户到Excel",notes = "date格式yyyy-MM-dd，默认查10条数据")
    @GetMapping("/DownloadDaily")
    public void DownloadDaily(String date,HttpServletResponse response) throws IOException {

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date1 = LocalDate.parse(date,dateTimeFormatter);
        LocalDateTime theDay = date1.atStartOfDay();

        flowDayService.getAllMultiStationDayForm(theDay,response);

    }

    /**
     * zz
     * @param date
     * @param stationNo
     * @return
     */
    @ApiOperation(value = "多用户信息，多用户日报表，根据站号、日期查询多用户中日报表",notes = "date格式yyyy-MM-dd")
    @GetMapping("/getMultiStationDayFormByStation")
    public Result<Map<String,Object>> getMultiStationDayFormByStation(String date,Integer stationNo){

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date1 = LocalDate.parse(date,dateTimeFormatter);
        LocalDateTime theDay = date1.atStartOfDay();

        Map<String,Object> resMap =  flowDayService.getMultiStationDayFormByStation(theDay,stationNo);

        return Result.success(resMap);
    }

    /**
     * zz
     * @param month
     * @param year
     * @param pageNo
     * @return
     */
    @ApiOperation(value = "多用户信息，多用户月报表，分页查询全部的多用户月报表",notes = "可以查询本月，默认查10条数据")
    @GetMapping("/getMultiStationMonthFormPage")
    public Result<PagedResult> getMultiStationMonthFormPage(Integer month,Integer year,Integer pageNo){

        LocalDateTime theDay = LocalDateTime.of(year,month,1,0,0,0);

        PagedResult pagedResult =  flowMonthService.getMultiStationMonthFormPage(theDay,pageNo);

        return Result.success(pagedResult);
    }

    @ApiOperation(value = "多用户信息，多用户月报表，导出全部的用户到Excel")
    @GetMapping("/DownloadMonthly")
    public void DownloadMonthly(Integer month,Integer year,HttpServletResponse response) throws IOException {

        LocalDateTime theDay = LocalDateTime.of(year,month,1,0,0,0);

        flowMonthService.getMultiStationMonthForm(theDay,response);

    }



    /**
     * zz
     * @param month
     * @param year
     * @param stationNo
     * @return
     */
    @ApiOperation(value = "多用户信息，多用户月报表，根据站号查询的多用户月报表",notes = "可以查询本月")
    @GetMapping("/getMultiStationMonthFormByStation")
    public Result<Map<String,Object>> getMultiStationMonthFormByStation(Integer month,Integer year,Integer stationNo){

        LocalDateTime theDay = LocalDateTime.of(year,month,1,0,0,0);

        Map<String,Object> res =  flowMonthService.getMultiStationMonthFormByStation(theDay,stationNo);

        return Result.success(res);
    }

    /**
     * zz
     * @param year
     * @param pageNo
     * @return
     */
    @ApiOperation(value = "多用户信息，多用户年报表，分页查询多用户年报表",notes = "可以查询本年")
    @GetMapping("/getMultiStationYearFormPage")
    public Result<PagedResult> getMultiStationYearFormPage(Integer year,Integer pageNo){

        LocalDateTime theDay = LocalDateTime.of(year,1,1,0,0,0);

        PagedResult pagedResult =  flowMonthService.getMultiStationYearFormPage(theDay,pageNo);

        return Result.success(pagedResult);
    }

    @ApiOperation("多用户信息，多用户年报表，根据站号查询的多用户年报表")
    @GetMapping("/getSingleStationYearForm")
    public Result<Map<String,Object>> getSingleStationYearForm(Integer Station_no, Integer year){


        Map<String,Object> resMap = new HashMap<>();

        resMap.put("用户站号",Station_no);
        resMap.put("用户站名",this.stationInfoService.getById(Station_no).getStationName());
        resMap.put("每月用汽集合",flowMonthService.getStationYearForm(Station_no, year));

        return Result.success(resMap);

    }

    @ApiOperation(value = "多用户信息，多用户年报表，导出全部的用户到Excel")
    @GetMapping("/DownloadYearly")
    public void DownloadYearly(Integer year,HttpServletResponse response) throws IOException {

        LocalDateTime theDay = LocalDateTime.of(year,1,1,0,0,0);

        flowMonthService.getMultiStationYearForm(theDay,response);

    }



    @ApiOperation(value = "//修改本月结算汽量并保存",notes = "前端可以设计成失焦就保存")
    @PostMapping("/updateMultiStationMonthFlowSettle")
    public Result<?> updateMultiStationMonthFlowSettle(Integer year,Integer month, Float flow,Integer stationNo){

        LocalDateTime theDay = LocalDateTime.of(year,month,1,0,0,0);

        Integer change =  flowMonthService.updateMultiStationMonthFlowSettle(theDay,flow,stationNo);
        if (change ==1){
            return Result.success("修改成功");
        }else{
            return Result.fail("修改失败");
        }

    }

    /**
     * 月度同比分析,分页查找所有
     * @param month
     * @param year
     * @param pageNo
     * @return
     */
    @ApiOperation(value = "多用户信息，月度同比分析,分页查找所有",notes = "可以查询本月，默认查10条数据")
    @GetMapping("/getMultiStationTBMonthFormPage")
    public Result<PagedResult> getMultiStationTBMonthFormPage(Integer month,Integer year,Integer pageNo){

        LocalDateTime theDay = LocalDateTime.of(year,month,1,0,0,0);

        PagedResult pagedResult =  flowMonthService.getMultiStationTBMonthFormPage(theDay,pageNo);

        return Result.success(pagedResult);
    }


    /**
     * 月度同比分析,根据站号查找一条数据
     * @param month
     * @param year
     * @param stationNo
     * @return
     */
    @ApiOperation(value = "多用户信息，月度同比分析,根据站号查找一条数据",notes = "可以查询本月")
    @GetMapping("/getMultiStationTBMonthFormByStation")
    public Result<Map<String,Object>> getMultiStationTBMonthFormByStation(Integer month,Integer year,Integer stationNo){

        LocalDateTime theDay = LocalDateTime.of(year,month,1,0,0,0);

        Map<String,Object> res =  flowMonthService.getMultiStationTBMonthFormByStation(theDay,stationNo);

        return Result.success(res);
    }







}
